package cn.snmp.mutual.uk.stack;

class Transmitter implements Runnable {
    private static final String version_id = "@(#)$Id: Transmitter.java,v 3.15 2007/02/05 15:01:20 birgita Exp $ Copyright Westhawk Ltd";
    Pdu pdu;
    Thread me;
    String myName;

    Transmitter(final String name) {
        this.pdu = null;
        this.me = new Thread(this, name);
        final Thread me = this.me;
        final Thread me2 = this.me;
        me.setPriority(1);
        if (AsnObject.debug > 12) {
            System.out.println("Transmitter(): Made thread " + name);
        }
        this.myName = name;
        this.me.start();
    }

    public void run() {
        while (this.me != null) {
            this.sit();
            synchronized (this) {
                if (this.pdu == null) {
                    continue;
                }
                this.pdu.transmit();
                this.pdu = null;
            }
        }
    }

    @Override
    public String toString() {
        final StringBuffer buffer = new StringBuffer(this.getClass().getName());
        buffer.append("[");
        buffer.append("name=").append(this.myName);
        buffer.append("]");
        return buffer.toString();
    }

    synchronized void setPdu(final Pdu p) {
        this.pdu = p;
    }

    synchronized void sit() {
        while (this.me != null && this.pdu == null) {
            try {
                this.wait(30000L);
            } catch (InterruptedException iw) {
                iw.printStackTrace();
            }
        }
    }

    synchronized void stand() {
        this.notifyAll();
    }

    void interruptMe() {
        if (this.me != null) {
            this.me.interrupt();
        }
    }

    void destroy() {
        this.me = null;
        this.pdu = null;
        this.stand();
    }
}
